package com.twohand.admin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twohand.admin.Dto.QrcodeListQueryDto;
import com.twohand.admin.Dto.SalesListQueryDto;
import com.twohand.admin.Entity.Qrcode;
import com.twohand.admin.Entity.Salesman;
import com.twohand.admin.Mapper.QrcodeMapper;
import com.twohand.admin.Mapper.SalesmanMapper;
import com.twohand.admin.Service.QrcodeService;
import com.twohand.admin.Service.SalesmanService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.utils.Response.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QrcodeServiceImpl implements QrcodeService {
    @Autowired
    QrcodeMapper qrcodeMapper;
    @Override
    public PageResponse<Qrcode> qrcodeList(QrcodeListQueryDto qrcodeListQueryDto) {
        PageHelper.startPage(qrcodeListQueryDto.getPageNum(),qrcodeListQueryDto.getPageSize());
        QueryWrapper<Qrcode> queryWrapper = new QueryWrapper<>();
        String remark = qrcodeListQueryDto.getRemark();
        if(remark != null && !remark.isEmpty() && !remark.trim().equals("")){
            queryWrapper.like("remark",remark);
        }
        queryWrapper.orderByDesc("is_default");
        List <Qrcode> qrcodeList = qrcodeMapper.selectList(queryWrapper);
        PageInfo<Qrcode> swipePageInfo = new PageInfo<>(qrcodeList);
        return new PageResponse<Qrcode>(swipePageInfo,qrcodeList);
    }

    @Override
    public Boolean addQrcode(Qrcode qrcode) {
        return qrcodeMapper.insert(qrcode) > 0;
    }

    @Override
    public Boolean changeQrcode(Qrcode qrcode) {
        return qrcodeMapper.updateById(qrcode) > 0;
    }

    @Override
    public Boolean delQrcode(Integer id) {
        return qrcodeMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean setDefault(Integer id) {
        qrcodeMapper.allNoDefault();
        Qrcode qrcode = qrcodeMapper.selectById(id);
        qrcode.setIsDefault(1);
        return qrcodeMapper.updateById(qrcode) > 0;
    }

    @Override
    public Qrcode getDefault() {
        LambdaQueryWrapper<Qrcode> qrcodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        qrcodeLambdaQueryWrapper.eq(Qrcode::getIsDefault,1);
        Qrcode qrcode = qrcodeMapper.selectOne(qrcodeLambdaQueryWrapper);
        if(qrcode==null){
            throw new BaseException(500,"未设置收款码！");
        }
        return qrcode;
    }
}
